package com.yayohos.locationmanager.providers.locationprovider;


import com.yayohos.locationmanager.helper.UpdateRequest;
import com.yayohos.locationmanager.helper.continuoustask.ContinuousTask;

import ohos.app.Context;
import ohos.eventhandler.EventRunner;
import ohos.location.Location;
import ohos.location.Locator;
import ohos.location.LocatorCallback;

import java.util.Date;

/**
 * DefaultLocationSource
 *
 * @date 2021/5/26
 */
class DefaultLocationSource {
    static final String PROVIDER_SWITCH_TASK = "providerSwitchTask";
    private final Locator locationManager;
    private UpdateRequest updateRequest;
    private ContinuousTask cancelTask;

    DefaultLocationSource(
            Context context,
            ContinuousTask.ContinuousTaskRunner continuousTaskRunner,
            LocatorCallback locationListener) {
        locationManager = new Locator(context);
        updateRequest = new UpdateRequest(locationManager, locationListener);
        cancelTask = new ContinuousTask(EventRunner.create("newThread"), PROVIDER_SWITCH_TASK, continuousTaskRunner);
    }

    /**
     * 位置服务是否开启
     *
     * @return locationManager.isLocationSwitchOn()
     */
    boolean isLocationSwitchOn() {
        return locationManager.isLocationSwitchOn();
    }

    /**
     * 获取缓存的位置信息
     *
     * @return locationManager.getCachedLocation();
     */
    Location getLastKnownLocation() {
        return locationManager.getCachedLocation();
    }

    /**
     * stopLocating
     *
     * @param locationListener LocatorCallback 定位回调
     */
    void removeLocationUpdates(LocatorCallback locationListener) {
        locationManager.stopLocating(locationListener);
    }

    /**
     * 定位资源回收
     */
    void removeUpdateRequest() {
        updateRequest.release();
        updateRequest = null;
    }

    /**
     * 定位任务终止
     */
    void removeSwitchTask() {
        cancelTask.stop();
        cancelTask = null;
    }

    /**
     * switchTaskIsRemoved
     *
     * @return cancelTask 是否移除
     */
    boolean switchTaskIsRemoved() {
        return cancelTask == null;
    }

    /**
     * updateRequest是否
     *
     * @return updateRequest 是否移除
     */
    boolean updateRequestIsRemoved() {
        return updateRequest == null;
    }

    /**
     * getProviderSwitchTask
     *
     * @return ContinuousTask
     */
    ContinuousTask getProviderSwitchTask() {
        return cancelTask;
    }

    /**
     * getUpdateRequest
     *
     * @return UpdateRequest updateRequest
     */
    UpdateRequest getUpdateRequest() {
        return updateRequest;
    }

    /**
     * 根据缓存的定位信息，比对定位精度，时间来判断是否使用缓存定位信息
     *
     * @param location             location
     * @param acceptableTimePeriod acceptableTimePeriod
     * @param acceptableAccuracy   acceptableAccuracy
     * @return boolean 是否使用缓存的定位
     */
    boolean isLocationSufficient(Location location, long acceptableTimePeriod, float acceptableAccuracy) {
        if (location == null) {
            return false;
        }
        float givenAccuracy = location.getAccuracy();
        long givenTime = location.getTimeStamp();
        long minAcceptableTime = new Date().getTime() - acceptableTimePeriod;
        return minAcceptableTime >= givenTime && acceptableAccuracy >= givenAccuracy;
    }
}
